﻿@using CodeGenerator
@using RazorLight
@using BXJG.Common;
@inherits TemplatePage<ExecuteContext>
@{
    DisableEncoding = true;//禁止html编码，因为我们是做代码生成器
}
using @(Model.Model.CoreNamespace);
using @Model.Model.CoreShareNamespace;
using @(Model.Model.ApplicationCommonShareNamespace);
using @(Model.Model.ApplicationShareNamespace);
using Abp.Authorization;
using BXJG.Utils.Application.Share.Dtos;

namespace @(Model.Model.ApplicationNamespace)
{
    /// <summary>
    /// @(Model.Model.DisplayName) 应用服务
    ///</summary>
    public partial class @(Model.Model.ApplicationServiceName) : AdminTreeCrudBaseAppService<@(Model.Model.EntityName), @(Model.Model.DtoName), @(Model.Model.CreateDtoName), @(Model.Model.EditDtoName), @(Model.Model.ConditionName)>
    {
        //提供权限名称
        protected override string GetPermissionName => @(Model.Model.PermissionNameGetConst);
        protected override string CreatePermissionName => @(Model.Model.PermissionNameCreateConst);
        protected override string UpdatePermissionName => @(Model.Model.PermissionNameUpdateConst);
        protected override string DeletePermissionName => @(Model.Model.PermissionNameDeleteConst);
        
        //protected override async ValueTask MapToEntity(@(Model.Model.EntityName) entity)
        //{
        //     await base.MapToEntity(input);
        //     新增和修改时，通过dto映射到entity之后都会调用，通常在这里对entity做进一步设置
        //}
       
        //protected override async Task DeleteCore(@(Model.Model.EntityName) entity)
        //{
        //      await base.DeleteCore(entity);
        //      删除的核心逻辑，通常在这里
        //}

        //protected override async Task<@(Model.Model.EntityName)> GetEntityByIdAsync(long id, bool track = true)
        //{
        //    return await base.GetEntityByIdAsync(id, track);
        //    crud都会调用，获取单个实体，通常在这里Include更多导航属性，另外参考BuildQuery方法
        //}

        protected override IQueryable<@(Model.Model.EntityName)> GetAllFilter(IQueryable<@(Model.Model.EntityName)> q, @(Model.Model.ConditionName) input, string parentCode)
        {
            q = base.GetAllFilter(q, input, parentCode);
@if(Model.Model.ConditionStringFields.Any())
{
    var idx = 0;
            @:q = q.WhereIf(input.Keywords.IsNotNullOrWhiteSpaceBXJG(), x => 
@foreach (var field in Model.Model.ConditionStringFields)
{
            if (idx>0)
            {
                @:|| x.@(field.Name).Contains(input.Keywords)
            }else{
                @:x.@(field.Name).Contains(input.Keywords)
            }
    idx++; 
}
            @:);
}

@foreach (var field in Model.Model.ConditionRangeFields)
{
            @:q = q.WhereIf(input.@(field.ConditionRangeMin).HasValue,x=> x.@(field.Name) >= input.@(field.ConditionRangeMin).Value );
            @:q = q.WhereIf(input.@(field.ConditionRangeMax).HasValue,x=> x.@(field.Name) < input.@(field.ConditionRangeMax).Value );
}

@foreach (var field in Model.Model.ConditionNormalFields)
{
            @:q = q.WhereIf(input.@(field.Name).HasValue,x=> x.@(field.Name) == input.@(field.Name).Value );
}
            return q;
        }

        //protected override IQueryable<@(Model.Model.EntityName)> BuildQuery(bool track = true)
        //{
        //     return base.BuildQuery(track);
        //     通常在这里Include获取单个数据和列表时都要的导航属性
        //}
    }
}